package cn.gok.manage.web;

import cn.gok.manage.service.UserService;
import cn.gok.pojo.User;
import cn.gok.util.MD5Util;
import cn.gok.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

/**
 * @author: 鸭鸭
 * @ClassName:UserController
 * @Package:cn.gok.manage.web
 * @Description:
 * @date: 2021/8/4
 * @time: 10:00
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    /*
    * 用户登录
    * */
    @RequestMapping("/login.do")
    @ResponseBody
    public ResultData getUserNameAndPwd(@RequestParam(defaultValue = "lisi")String username,
                                     @RequestParam(defaultValue = "lisi")String password){

        try {
            String pwdJM = MD5Util.encoderPassword(password);
            return userService.userLogin(username,pwdJM);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.fail("用户名或密码错误");
        }
    }

    /*
    * 用户注册
    * */
    @RequestMapping("register.do")
    @ResponseBody
    public ResultData userRegister(@RequestParam(defaultValue = "lisi")String username,
                                   @RequestParam(defaultValue = "lisi")String password,
                                   @RequestParam(defaultValue = "11223344@qq.com")String email,
                                   @RequestParam(defaultValue = "19960106666")String phone,
                                   @RequestParam(defaultValue = "我的名字")String question,
                                   @RequestParam(defaultValue = "李四")String answer) {
        ResultData resultData = userService.selectByUserName(username);
        if(resultData.getStatus().equals("1")){
            return resultData;
        }else{
            try {
                String pwdJM = MD5Util.encoderPassword(password);
                Date d = new Date();
                User user = new User(username,pwdJM,email,phone,question,answer,1,d,d);
                return userService.userRegister(user);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultData.fail("注册失败！");
            }
        }
    }
    /*
    * 检查用户名是否有效
    * */
    @RequestMapping("check_valid.do")
    @ResponseBody
    public ResultData checkVaild(@RequestParam(defaultValue = "lisi")String str,
                                 @RequestParam(defaultValue = "username")String type){
        return userService.checkVaild(str,type);
    }


    /*
    * 获取登录用户信息
    * */
    @RequestMapping("get_user_info.do")
    @ResponseBody
    public ResultData GetUserInfo(@RequestParam(defaultValue = "lisi")String username){
        return userService.selectAllByName(username);
    }

    /*
    * 忘记密码，查询问题
    * */
    @RequestMapping(value = "forget_get_question.do",method = RequestMethod.GET)
    @ResponseBody
    public ResultData forgetGetQuestion(@RequestParam("username") String username){
        return userService.forgetPwd(username);
    }

    /*
    * 提交答案，验证答案是否正确
    * */
    @RequestMapping(value = "forget_check_answer.do",method = RequestMethod.GET)
    @ResponseBody
    public ResultData forgetCheckAnswer(@RequestParam("username") String username,
                                        @RequestParam("question") String question,
                                        @RequestParam("answer") String answer){

        return userService.sendPwdAnswer(username,question,answer);
    }

    /*
    * 忘记密码的重设密码
    * */
    @RequestMapping(value = "forget_reset_password.do",method = RequestMethod.GET)
    @ResponseBody
    public ResultData forgetResetPassword(@RequestParam("password") String password,
                                          @RequestParam("username") String username) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String pwdJM = MD5Util.encoderPassword(password);
        return userService.updatePwdByAnswer(pwdJM,username);
    }

    /*
     * 登录中状态重置密码
     * */

    @RequestMapping(value = "reset_password.do",method = RequestMethod.GET)
    @ResponseBody
    public ResultData resetPasswordLogin(@RequestParam("passwordNew") String passwordNew,@RequestParam("passwordOld") String passwordOld) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String pwdOldJM = MD5Util.encoderPassword(passwordOld);
        String pwdNewJM = MD5Util.encoderPassword(passwordNew);
        String username = "soonerbetter";
        return userService.updatePwdLogin(pwdNewJM,pwdOldJM,username);
    }

    /*
     * 登录状态更新个人信息
     * */
    @RequestMapping(value = "update_information.do",method = RequestMethod.GET)
    @ResponseBody
    public ResultData UpdateInformation(@RequestParam("email") String email,
                                        @RequestParam("phone") String phone,
                                        @RequestParam("question") String question,
                                        @RequestParam("answer") String answer){
        String username="WangWu";
        User user = new User();
        user.setUsername(username);
        user.setEmail(email);
        user.setPhone(phone);
        user.setQuestion(question);
        user.setAnswer(answer);
        user.setUpdate_time(new Date());
        return userService.updateInfoByName(user);
    }

    /*
     *获取当前登录用户的详细信息，并强制登录
     */
    @RequestMapping(value = "get_information.do",method = RequestMethod.GET)
    @ResponseBody
    public ResultData getInformationByName(){
        String username = "WangWu";
        return userService.getAllInfoByName(username);
    }

    /*
     * 退出登录
     * */


    /*
    * 用户列表
    * */

    @ResponseBody
    @RequestMapping("list.do")
    public ResultData getAllUser(@RequestParam(defaultValue = "1")int pageNum,
                                 @RequestParam(defaultValue = "10") int pageSize){

        return userService.queryAllUser(pageNum,pageSize);
    }
}
